﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO

'From Custom Classes/Reference
'None

#End Region

Namespace DrawingCEEx.SenseAPIsEx

    Public Class SenseStrings

        ''' <summary>
        ''' CalculateStringHeight
        ''' </summary>
        ''' <param name="__String"></param>
        ''' <param name="__FontName"></param>
        ''' <param name="__FontSize"></param>
        ''' <param name="__FontStyle"></param>
        ''' <param name="__Size"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CalculateStringHeight(ByVal __String As String, ByVal __FontName As String, ByVal __FontSize As Integer, ByVal __FontStyle As FontStyle, ByVal __Size As Size) As Integer

            Try

                If __String Is Nothing Or __String = String.Empty Then Exit Function

                Dim _Str As String = Nothing

                If __FontName Is Nothing Then __FontName = "Tahoma"
                If __FontSize = Nothing Then __FontSize = 8
                If __FontStyle = Nothing Then __FontStyle = FontStyle.Regular

                Select Case __String.Length

                    Case 0 To 100
                        _Str = __String
                    Case 101 To 200
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 20, 10), CChar("0"))
                    Case 201 To 300
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 40, 20), CChar("0"))
                    Case 301 To 400
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 60, 30), CChar("0"))
                    Case 401 To 500
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 80, 40), CChar("0"))
                    Case 501 To 600
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 100, 50), CChar("0"))
                    Case 601 To 700
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 120, 60), CChar("0"))
                    Case 701 To 800
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 140, 70), CChar("0"))
                    Case 801 To 900
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 160, 80), CChar("0"))
                    Case 901 To 1000
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 180, 90), CChar("0"))
                    Case Else
                        _Str = __String.PadRight(__String.Length + If(__FontSize > 6, 200, 100), CChar("0"))

                End Select

                Using _font As Font = New Font(__FontName, __FontSize, __FontStyle)
                    Using _bitmap As New Bitmap(1, 1)
                        Dim _graphics As Graphics = Graphics.FromImage(_bitmap)
                        Dim stringSize As SizeF
                        stringSize = _graphics.MeasureString(_Str, _font)
                        Dim p As Integer = CInt(Math.Ceiling((stringSize.Width + 1) / __Size.Width))
                        Dim v As Integer = CInt((__Size.Height / 2 - p * stringSize.Height) / 2)
                        Return CInt((__Size.Height / 2) - 1 - 2 * v) + 2
                    End Using
                End Using

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' CalculateCharactersCanFit
        ''' </summary>
        ''' <param name="__String"></param>
        ''' <param name="__FontName"></param>
        ''' <param name="__FontSize"></param>
        ''' <param name="__FontStyle"></param>
        ''' <param name="__Size"></param>
        ''' <param name="g"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CalculateCharactersCanFit(ByVal __String As String, ByVal __FontName As String, ByVal __FontSize As Integer, ByVal __FontStyle As FontStyle, ByVal __Size As Size, ByVal g As System.Drawing.Graphics) As Integer

            Try

                If __String Is Nothing Or __String = String.Empty Then Exit Function

                Dim str As String = Nothing
                Dim size As SizeF = Nothing
                Dim chars As Integer = 1

                Do While True

                    str = Left(__String, chars)
                    size = g.MeasureString(str, New Font(__FontName, __FontSize, __FontStyle))

                    If size.Width > (__Size.Width - 38) Then
                        Exit Do
                    End If

                    chars += 1

                    If chars = __String.Length Then Exit Do

                Loop

                Return chars

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        Public Shared Function GetTrimmedDottedText(ByVal __String As String, ByVal __FontName As String, ByVal __FontSize As Integer, ByVal __FontStyle As FontStyle, ByVal __Size As Size, ByVal g As System.Drawing.Graphics) As String

            Dim _charsCount As Integer = CalculateCharactersCanFit(__String, __FontName, __FontSize, __FontStyle, __Size, g)

            If __String.Length > _charsCount Then

                Return __String.Substring(0, _charsCount - 3) & "..."

            Else

                Return __String

            End If

        End Function

    End Class

End Namespace


